package com.xueshishasha.oa.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;

import com.xueshishasha.oa.common.utils.PageModel;
import com.xueshishasha.oa.common.utils.PaginationParas;
import com.xueshishasha.oa.model.Employee;
import com.xueshishasha.oa.model.Role;
import com.xueshishasha.oa.service.DataService;

public class EmployeeServiceImpl implements
		com.xueshishasha.oa.service.EmployeeService {

	private DataService<Employee> dataService ;

	/**
	 * 验证成功返回id 不成功返回null
	 */
	@Override
	public Employee authentication(Employee e) {
		if(e==null){
			return null;
		}
		if(e.getName().length()<6){
			return null;
		}
		String sql = "from Employee where name=? and password=?";
		Employee getE;
		try{
			getE = dataService.getModel(sql, new String[]{e.getName(),e.getPassword()});
			if(null != getE){//验证成功！
				getE.setOnline(true); //设置为在线状态
				dataService.saveOrUpdate(getE);
				
				return getE;//返回id值
			}
			return null;//返回null；
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean changePassword(String id, String oldpassword, String newpassword) {
		
		
		try{
			String sql = "from Employee where password = ? and id =?";
			Employee e = dataService.getModel(sql, new String[]{oldpassword,id});
			e.setPassword(newpassword);
			dataService.save(e);
			return true;
		}catch(Exception ex){
			return false;
		}
	}

	
	@Override
	public Employee getInfo(String id) {
		return this.getModelById(id);
	}

	@Override
	public Boolean logout(String id) {
		try{
			Employee e = dataService.getModelById(Employee.class, id);
			e.setOnline(false);//离线
			dataService.saveOrUpdate(e);
			return true;
		}catch(Exception e){
			return false;
		}
	}

	@Override
	public Serializable register(Employee e) {
		return this.save(e);
	}

	@Override
	public void updateInfo(Employee e) {
		this.update(e);
	}
	
	@Override
	public Serializable save(Employee e){
		return dataService.save(e);
		
	}

	@Override
	public void delete(Employee e) {
		dataService.delete(e);

	}

	@Override
	public List<Employee> getAll() {
		String sql = "from Employee";
		return dataService.getModels(sql,null);
	}

	@Override
	public Employee getModelById(Serializable id) {
		return dataService.getModelById(Employee.class, id);
	}

	@Override
	public List<Employee> getModels(String sql, Object[] parameters) {
		return dataService.getModels(sql,parameters);
	}

	@Override
	public void saveOrUpdate(Employee e) {
		dataService.saveOrUpdate(e);
	}

	@Override
	public void saveOrUpdateAll(Collection<Employee> ce) {
		dataService.saveOrUpdateAll(ce);
	}

	@Override
	public void update(Employee e) {
		dataService.update(e);
	}
	
	@Override
	public List<Employee> getByCriteria(DetachedCriteria dc) {
		return dataService.getByCriteria(dc);
	}

	public DataService<Employee> getDataService() {
		return dataService;
	}

	public void setDataService(DataService<Employee> dataService) {
		this.dataService = dataService;
	}

	@Override
	public Long getCount() {
		return dataService.getCount(Employee.class);
	}
	


	@Override
	public  List<Employee> findAllByCriteria(DetachedCriteria detachedCriteria) {
		return (List<Employee>) this.getDataService().findAllByCriteria(detachedCriteria);
	}

	
	//----------------------------分页查询--------------------------
	
	//
	public PageModel list(final String hql, final Object[] paras,
			final int offset, final int count){
		return dataService.list(hql, paras, offset, count);
	}
	
	
	public PageModel listByPage( String hql,  Object[] paras,
			 int pageIndex,  int count){
		System.out.println(hql+","+paras+","+pageIndex+","+count);
		if(pageIndex<1){
			pageIndex = 1; 
		}
		if(count<1){
			count=10;
		}
		return dataService.list(hql, paras,(pageIndex-1)*count, count);
	}

	@Override
	public PageModel listByPage(PaginationParas p) {
		return dataService.list(p.getHql(), p.getParas(),(p.getPageIndex()-1)*p.getCount(), p.getCount());
	}
	public List getFlexObejct(){
		Employee e = new Employee();
		e.setName("xueshi");
		e.setPassword("shasha");
		Role role = new Role();
		role.setName("总裁");
		e.setRole(role);
		
		Employee e2 = new Employee();
		e2.setName("xueshi");
		e2.setPassword("shasha");
		e2.setRole(role);
		
		List arrayList = new ArrayList();
		arrayList.add(e);
		arrayList.add(e2);
		return arrayList;
	}
	
	
	
	
	
}
